package com.haoze.nurseapp;

import com.alibaba.druid.support.http.StatViewServlet;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import tk.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.concurrent.Executor;

@EnableFeignClients(basePackages = {"com.haoze"})
@EnableCaching
@EnableScheduling
@SpringBootApplication(scanBasePackages = {"com.haoze.nurseapp","com.haoze.common"})
@EnableTransactionManagement(proxyTargetClass = true)
@MapperScan(basePackages = "com.haoze.nurseapp.dao")
public class CloudNurseappApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudNurseappApplication.class, args);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public ServletRegistrationBean statViewServlet() {
        return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
    }

    @Value("${spring.mongodb}")
    private String mongoDbUrl;

    @Bean
    public MongoClient mongoClient() {
        MongoClientURI connectionString = new MongoClientURI(mongoDbUrl);
        return new MongoClient(connectionString);
    }

    @Bean("taskExecutor")
    public Executor taskExecutor(){
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(10);
        taskExecutor.setMaxPoolSize(50);
        taskExecutor.setQueueCapacity(500);
        taskExecutor.setKeepAliveSeconds(60);
        taskExecutor.setThreadNamePrefix("taskExecutor--");
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        taskExecutor.setAwaitTerminationSeconds(60);
        return taskExecutor;
    }

    @Bean
    public MongoTemplate doctorMongoTemplate() {
        return new MongoTemplate(mongoClient(), "doctor");
    }

}
